package com.tbit.main.util;

import com.tbit.main.pojo.TerPosition;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author LMY
 * @create 2020-12-29 9:07
 * 集合工具类
 */
@Slf4j
public class CollectionUtil {

    /**
     * List 倒叙
     *
     * @param list
     */
    public static void flashbackList(List<TerPosition> list) {
        Collections.sort(list, new Comparator<TerPosition>() {
            @Override
            public int compare(TerPosition o1, TerPosition o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date dt1 = format.parse(o1.getDt());
                    Date dt2 = format.parse(o2.getDt());
                    if (dt1.getTime() < dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() > dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    LogUtil.error("排列时间报错", e);
                }
                return 0;
            }
        });
    }

    /**
     * list 升序
     *
     * @param list
     */
    public static void ascendingList(List<TerPosition> list) {
        Collections.sort(list, new Comparator<TerPosition>() {
            @Override
            public int compare(TerPosition o1, TerPosition o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date dt1 = format.parse(o1.getDt());
                    Date dt2 = format.parse(o2.getDt());
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    LogUtil.error("排列时间报错", e);
                }
                return 0;

            }
        });
    }

    public static void main(String[] args) {
        List<TerPosition> list = new LinkedList<>();
        TerPosition t1 = new TerPosition();
        t1.setDt("2020-12-21 01:00:00");
        t1.setMachineNO("1");
        TerPosition t2 = new TerPosition();
        t2.setDt("2020-12-22 02:00:00");
        t2.setMachineNO("2");
        TerPosition t3 = new TerPosition();
        t3.setDt("2020-12-23 03:00:00");
        t3.setMachineNO("3");
        list.add(t1);
        list.add(t2);
        list.add(t3);

        flashbackList(list);

        System.out.println("倒叙：" + list);

        ascendingList(list);
        System.out.println("升序：" + list);


    }
}
